const express = require("express");
const path = require("path");
const recipesRouter = require("./routers/recipes");
const usersRouter = require("./routers/user");
const {Logger} = require("./common/util/log")
const cors = require("cors");
const {handleError} = require("./common/util/error");
const auth = require("./middleware/auth");
const publicPath = path.join(__dirname, "./public");
const app = express();


app.use(cors());
app.use(express.static(publicPath));
// 记日志
app.use((req, res, next) => {
  Logger.info(req.method, req.path);
  next();
});
// 特别参数处理
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 进行认证
app.use(auth.initialize());

// 绑定路由
app.use("/api/v1/recipes", recipesRouter);
app.use("/api/v1/users",usersRouter)

// 设置异常处理
app.use(handleError);

const port = process.env.port || 3000;
app.listen(port, () => {
  Logger.info(`Server is running on port ${port}`);
});
